#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000//通讯录中最大的存储人数
struct address_book//定义结构体类型 
{
	int num;
	char name[40];
	char occu[40];//职业 
	int tel;
	int email; 
	char address[40]; //地址 
}; 
//增添
int menu1(address_book a[],int n,int m);
//删除
int del(address_book a[],int n,int k);
int menu2(address_book a[],int n);
//修改
int add1(address_book a[],int n,int i);
void update(address_book a[],int n,int k);
void menu3(address_book a[],int n);
//查询
void menu4(address_book a[],int n);
void findname(address_book a[],int n,char Fname[]);
void findnum(address_book a[],int n,int Fnum);
void search(address_book a[],int i);
//排序
void menu5(address_book a[],int n);
void sortSname(address_book a[],int n);
void traversal(address_book a[],int n);
int main()
{
	address_book a[MAX];
	 int n=0;
	
	while(1)
	{
	system("cls");//清屏。
	printf("\n\n\n\n\n");
	printf("*****通讯录管理系统*****\n");
	printf("**0.退出系统          **\n");
	printf("**1.增添联系人        **\n");
	printf("**2.删除联系人        **\n");
	printf("**3.修改联系人        **\n");
	printf("**4.查询联系人        **\n");
	printf("**5.联系人排序        **\n");
	printf("**6.显示所有联系人    **\n"); 
	printf("************************\n");
	printf("***请选择(0-6): ");
	int option;
	scanf("%d",&option);
	switch(option)
	{
		case 0:
			{
				return 0;
			}
			case 1:
			{
				int m;
				system("cls");
				scanf("%d",&m);
				n=menu1(a,n,m);
				break;
			}
			case 2:
			{
				n=menu2(a,n);
				break;
			}
			case 3:
			{
			menu3(a,n);
			break;	
			}
			case 4:
				{
					menu4(a,n);
					break;
				}
				case 5:
					{
						menu5(a,n);
						break;
					}
					case 6:
						{
							system("CLS");
						    traversal(a,n);
						    break;	
						}
						default:continue;
				
	}
	system("pause");
}
return 0;
 } 
 //添加
int menu1(address_book a[],int n,int m)
{
	for(int i=n;i<n+m;i++)
	{
		printf("num: \n");
		scanf("%d",&a[i].num);
		fflush(stdin);
		printf("姓名: \n");
		gets(a[i].name);
		printf("职业：\n");
		gets(a[i].occu);
		printf("电话：\n");
		scanf("%d",&a[i].tel);
		printf("邮箱：\n");
		scanf("%d",&a[i].email);
		fflush(stdin);
		printf("地址：\n");
		gets(a[i].address);
		printf("增添成功\n");
	}
	n=n+m;
	return n;
}
//删除
int del(address_book a[],int n,int k)
{
	for(int i=0;i<n;i++)
	{
		if(a[i].num==k)
		{
			for(int j=i;j<n;j++)
			{
				a[j]=a[j+1];
			}
			break;
		}
	}
	n=n-1;
	return n;
}
int menu2(address_book a[],int n)
{
	while(1)
	{
		system("cls");
		printf("\n\n\n");
		printf("***************\n");
		printf("**删除联系人***\n");
		printf("**0.退出    ***\n");
		printf("**1.删除联系人*\n");
		printf("***************\n");
		printf("**请选择(0-1): ");
		int option;
		scanf("%d",&option);
		switch(option)
		{
			case 0:
				{
					return n;
				}
				case 1:
					{
						int i;
						int k;
						printf("请输入指定联系人姓名：\n");
						scanf("%s",&a[i].name);
						n=del(a,n,k);
						printf("删除成功！\n");
						system("pause");
						break;
					}
		}
	}
	return n;
}

//修改
int add1(address_book a[],int n,int i)
{
	printf("num: \n");
		scanf("%d",&a[i].num);
		fflush(stdin);
		printf("姓名: \n");
		gets(a[i].name);
		printf("职业：\n");
		gets(a[i].occu);
		printf("电话：\n");
		scanf("%d",&a[i].tel);
		printf("邮箱：\n");
		scanf("%d",&a[i].email);
		fflush(stdin);
		printf("地址：\n");
		gets(a[i].address);
		printf("\n");
		return n;
}
void update(address_book a[],int n,int k)
{
	address_book temp[1];
	int x=0;
	x=add1(temp,x,0);
	for(int i=0;i<n;i++)
	{
		if(a[i].num==k)
		{
			a[i]=temp[0];
			break;
		}
	}
	return;
}

void menu3(address_book a[],int n)
{
	while(1)
	{
		system("cls");
		printf("***************\n");
		printf("**修改联系人***\n");
		printf("**0.退出       \n");
		printf("**1.修改联系人 \n");
		printf("***************\n");
		printf("**请选择(0-1): ");
		int option;
		scanf("%d",&option);
		switch(option)
		{
			case 0:
				{
					return;
				}
				case 1:
					{
						int i;
						int k;
						printf("请输入指定联系人序号：\n");
						scanf("%d",&k);
						update(a,n,k);
						printf("修改成功！\n");
						system("pause");
						break;
					}
					default:continue;
			
		}
	}
	return;
}


//查询
void menu4(address_book a[],int n)
{
	while(1)
	{
		system("cls");
		printf("********************\n");
		printf("**查询联系人********\n");
		printf("**0.退出            \n");
		printf("**1.按姓名查找联系人\n");
		printf("**2.按序号查找联系人\n");
		printf("********************\n");
		printf("**请选择(0-2): ");
		int option;
		scanf("%d",&option);
		switch(option)
		{
			case 0:
				{
					return;
				}
				case 1:
					{
						char name[40];
						printf("请输入查询的姓名：");
						scanf("%s",&name);
						findname(a,n,name);
						system("pause");
						break;
					}
					case 2:
						{
							int num;
							printf("请输入查询的序号：");
							scanf("%d",&num);
							findnum(a,n,num);
							system("pause");
							break;
						}
		}
	}
	return;
}
void findname(address_book a[],int n,char Fname[])
{
	for(int i=0;i<n;i++)
	{
		if(strcmp(a[i].name,Fname)==0)
		{
			search(a,i);
		}
	}
	return;
}
void findnum(address_book a[],int n,int Fnum)
{
	for(int i=0;i<n;i++)
	{
		if(a[i].num==Fnum)
		{
			search(a,i);
		}
	}
	return;
}
void search(address_book a[],int i)
{
	printf("num： %d\n",a[i].num);
	printf("姓名：%s\n",a[i].name);
	printf("职业：%s\n",a[i].occu);
	printf("电话：%d\n",a[i].tel);
	printf("邮箱：%d\n",a[i].email);
	printf("地址：%s\n",a[i].address);
	printf("\n");
	return;
}
//排序
void menu5(address_book a[],int n)
{
	while(1)
	{
		system("cls");
		printf("**************\n");
		printf("**联系人排序**\n");
		printf("**0.退出      \n");
		printf("**1.按姓名排序\n");
		printf("**************\n");
		printf("**请输入(0-1): ");
		int option;
		scanf("%d",&option);
		switch(option)
		{
			case 0:
				{
					return;
				}
				case 1:
					{
						sortSname(a,n);
						system("pause");
						break;
					}
				
		}
	}
	return;
}
void sortSname(address_book a[],int n)
{
	address_book name[n];
	for(int i=0,j=0;i<n;i++,j++)
	{
		name[j]=a[i];
	}
	address_book temp;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n-1;j++)
		{
			if(strcmp(name[j].name,name[j+1].name)>0)
			{
				temp=name[j];
				name[j]=name[j+i];
				name[j+1]=temp;
			}
		}
	}
	traversal(name,n);
	return;
}
void traversal(address_book a[],int n)
{
	for(int i=0;i<n;i++)
	{
		printf("num: %d\n",a[i].num);
		printf("姓名：%s\n",a[i].name);
		printf("职业：%s\n",a[i].occu);
		printf("电话：%d\n",a[i].tel);
		printf("邮箱：%d\n",a[i].email);
		printf("地址：%s\n",a[i].address);
		printf("\n");
	}
	return;
}



